#ifndef APPMANAGER_H
#define APPMANAGER_H

#include <QMainWindow>
#include <QTabWidget>
#include <QPushButton>
#include "configfilereader.h"
#include <QLineEdit>
#include <QLabel>
#include <QTimer>
#include <windows.h>
#include <QProcess>
#include <QSystemTrayIcon>// 托盘类
#include <QDesktopServices>// 桌面事件类
#include <QFileIconProvider>
#include <QTemporaryFile>
#include <QPicture>
#include <globalControl.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QResizeEvent>
#include <QToolButton>
#include <QFileInfo>
#include <QMessageBox>
#include <tlhelp32.h>
#include <QDebug>
#include <QScrollArea>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QTextStream>
#include <QThread>
#include <cstdlib>
#include <iostream>
#include <QTabBar>
#include <QInputDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QImageReader>
#include <shlobj.h>
#include <QClipboard>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>





QT_BEGIN_NAMESPACE
namespace Ui { class AppManager; }
QT_END_NAMESPACE

typedef struct _process_id_pair{
    int pID1;
    int pID2;
}PROCESS_ID_PAIR;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
QString GetLinkTarget(const QString &shortcutPath);
QString convertWindowsToLinuxPath(const QString &windowsPath);

#ifndef DBFILE
    #define DBFILE (u8"appstate.db")
#endif



class AppManager : public QMainWindow
{
    Q_OBJECT


public:
    AppManager(QWidget *parent = nullptr);
    ~AppManager();
    bool init();
    bool checkProcessRunning(const QString &processName, int &processId);
    QString getExeName(QString str);
    int getProcessID(QString exename);
    void switchApplication(DWORD pid);
    void createTray();
    // QLabel* createLabel(QString name, int width);
    // QLabel* createIcon(QString filePath, int width);
    // QLineEdit* createLineEdit(QString text);
    // QLabel* createStatus();
    // QToolButton* createToolButton();
    // QPushButton* createSwitchButton();
    void addToError(QString line);
    QVBoxLayout* addNewTab(QString tabName = u8"新建标签页");
    void deleteTab(int index);
    QWidget* createAppLine(QString& name, QString lineEditText = "", bool flag = false);
    // QObject interface
    bool eventFilter(QObject *watched, QEvent *event) override;
    int getLineIndex(QObject *watched);
    QString getNewName(QString name);


private:
    void createDatabase(const QString &dbName);
    void insertData(const int index, const QString &appName, const int process_id1, const int process_id2);
    void updateData(const int index, const QString &appName, const int process_id1, const int process_id2);
    bool queryRunningStateByIndexFromDB(const int index);
    QString queryAppNameByIndexFromDB(const int index);
    int queryPID1ByIndexFromDB(const int index);
    int queryPID2ByIndexFromDB(const int index);
    bool isAppInDB(const int index);


signals:
    void startFinished();


public slots:
    // void startPage();
    void startPage(int subIndex, QWidget* page);
    // void killPage();
    void killPage(int subIndex, QWidget* page);
    // void startAll();
    void startAll(int subIndex);
    // void killAll();
    void killAll(int subIndex);
    void killProcess(const QString& processName, const int& processId);
    void monitoring();
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
    QIcon fileExtensionIcon(const QString &filePath) const;

protected:
    void resizeEvent(QResizeEvent* event);
    void changeStatus(int index, bool runState);
    void closeEvent(QCloseEvent *event) override;
    void hideEvent(QHideEvent *event) override;


private:
    Ui::AppManager *ui;
    QTabWidget* pTab = nullptr;
    QLineEdit* pInputLineEdit = nullptr;
    QPushButton* startAllButton = nullptr;
    QPushButton* killAllButton = nullptr;
    QWidget* buttonArea = nullptr;
    QString mError;
    ConfigFileReader reader;
    QList<QLabel*> mLBList;
    QList<QPushButton*> mPBList;
    QList<QLineEdit*> mLEList;
    QList<QLabel*> mRunLBList;
    QList<QPushButton*> mDelBtnList;
    QList<QPushButton*> mSwPBList;
    QTimer timer;
    QList<PROCESS_ID_PAIR> mProcessIdList;
    QMap<int, QProcess*> processMap;
    QMenu *trayMenu = nullptr; // 托盘菜单
    QSystemTrayIcon *tray = nullptr; // 托盘图标添加成员
    // QAction* minimizeAction;
    QAction* restoreAction = nullptr;
    QAction* quitAction = nullptr;
    QList<quint8> checkCountList;
    int nameCount = 0;
    QSqlDatabase db;

};
#endif // APPMANAGER_H
